<?php
/**
 * Created by PhpStorm.
 * User: yu
 * Date: 2017/1/7
 * Time: 下午1:48
 */

namespace Admin\Controller;

use Common\Controller\AdminController;
use Common\Model\BrandModel;
use Common\Model\CategoryModel;
use Common\Model\GoodsModel;
use Common\Model\TypeAttrModel;
use Org\Data\Data;

class GoodsController extends AdminController
{
	protected $cateModel;
	protected $brandModel;
	protected $goodsModel;
	protected $cateTree;//分类树形数据
	public function __init(){
		$this->goodsModel = new GoodsModel();
		$this->cateModel = new CategoryModel();
		$data = $this->cateModel->select();
		$this->cateTree = (new Data())->tree($data,'cname');
		$this->brandModel = new BrandModel();

	}
	public function lists(){
		$goodsList = $this->goodsModel->select();
		$this->assign('goodsList',$goodsList);
		$this->display();
	}
	public function add(){
		if(IS_POST){
//			p($_POST);die;
			$re = $this->goodsModel->addGoods();
			if($re['status']=='success'){
				$this->success($re['message'],u('admin/goods/lists'));
			}else{
				$this->error($re['message']);
			}
		}else{

			//分配分类树形数据
			$this->assign('cateTree',$this->cateTree);
			//分配品牌数据
			$brandList = $this->brandModel->select();
			$this->assign('brandList',$brandList);
			$this->display();
		}
	}

	/**
	 * 异步ajax获得 tid对应的 属性列表
	 */
	public function getGoodsAttr(){
		$tid = I('get.tid');
		$class = I('get.class');
		$tpl = I('get.tpl');
		$goodsAttrData = (new TypeAttrModel())->where("tid=$tid&&class=$class")->select();
		foreach ($goodsAttrData as $k=>$v){
			$goodsAttrData[$k]['tavalue'] = explode(',',$v['tavalue']);
		}

		$this->assign('goodsAttrData',$goodsAttrData);
		echo $this->fetch($tpl);die;
	}
}